<template>
  <div class="chart-container">
    <div :id="id" :class="className" :style="{ height: height, width: width }"></div>
    <el-button type="primary" class="chart-button" @click="handleButtonClick">确定</el-button>
    <div class="dateSelect">
      <span>日期</span>
      <el-date-picker style="margin-left: 20px;" v-model="selectedDate" type="date" placeholder="选择日期"
        @change="filterByDate" @clear="resetFilter">
      </el-date-picker>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { senddate } from '@/api/form'
//import resize from './mixins/resize'

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '1000px'
    },
    height: {
      type: String,
      default: '450px'
    },
  },
  data () {
    return {
      chart: null,
      buttonClicked: false,
      selectedDate: null,
      chartData: [] // 图表数据数组
    }
  },
  mounted () {
    this.initChart()
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    handleButtonClick () {
      // const date = new Date(this.selectedDate)
      // const year = date.getFullYear()
      // const month = date.getMonth() + 1
      // const day = date.getDate()
      // const formattedDate = `${year}-${month}-${day}`

      // senddate({ "time": formattedDate }).then(response => {
      const date = new Date(this.selectedDate)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, "0")
      const day = String(date.getDate()).padStart(2, "0")
      const formattedDate = `${year}-${month}-${day}`

      senddate({ "time": formattedDate }).then(response => {
        // this.buttonClicked = true;
        // 执行你想要的按钮点击操作
        // senddate({ "time": this.selectedDate }).then(response => {
        // 处理成功的响应
        this.chartData = response.data.data;
        this.initChart() // 重新初始化图表以应用新数据
        this.$message('update successfully!')
        // reponsecode = reponse.data["code"]
      }).catch(error => {
        // 处理错误的响应
        this.$message.error('Failed to update ')
      })
    },

    initChart () {
      const times = this.chartData.map(item => item.ctime);
      const peopleNums = this.chartData.map(item => item['people_num']);
      if (this.buttonClicked) {
        // 执行按钮点击相关的操作
      }
      this.chart = echarts.init(document.getElementById(this.id))
      const xData = (function () {
        const data = []
        for (let i = 2; i < 25; i = i + 2) {
          data.push(i + 'hour')
        }
        return data
      }())
      this.chart.setOption({
        backgroundColor: '#344b58',
        title: {
          // text: 'statistics',
          text: '入侵统计',
          x: '20',
          top: '20',
          textStyle: {
            color: '#fff',
            fontSize: '22'
          },
          subtextStyle: {
            color: '#90979c',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 150,
          bottom: 95,
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          x: '5%',
          top: '10%',
          textStyle: {
            color: '#90979c'
          },
          // data: ['female', 'male', 'average']
          data: ['熟人', '陌生人', 'total']
        },
        calculable: true,
        xAxis: [{
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitArea: {
            show: false
          },
          axisLabel: {
            interval: 0

          },
          // data: xData
          // ...代码未改动
          data: times // 使用时间数据

        }],
        yAxis: [{
          type: 'value',
          splitLine: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0
          },
          splitArea: {
            show: false
          }
        }],
        dataZoom: [{
          show: true,
          height: 30,
          xAxisIndex: [
            0
          ],
          bottom: 30,
          start: 10,
          end: 80,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: '#d3dee5'

          },
          textStyle: {
            color: '#fff'
          },
          borderColor: '#90979c'

        }, {
          type: 'inside',
          show: true,
          height: 15,
          start: 1,
          end: 35
        }],
        series: [
          {
            // name: 'male',
            name: '陌生人',
            type: 'bar',
            stack: 'total',
            itemStyle: {
              normal: {
                color: 'rgba(0,191,183,1)',
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: 'top',
                  formatter (p) {
                    return p.value > 0 ? p.value : ''
                  }
                }
              }
            },
            // data: this.strangerData // 使用从后端获取的陌生人数据
            data: peopleNums
            // data: [
            //   327,
            //   1776,
            //   507,
            //   1200,
            //   800,
            //   482,
            //   204,
            //   1390,
            //   1001,
            //   951,
            //   381,
            //   220
            // ]
          }, {
            // name: 'average',
            name: 'total',
            type: 'line',
            stack: 'total',
            symbolSize: 10,
            symbol: 'circle',
            itemStyle: {
              normal: {
                color: 'rgba(252,230,48,1)',
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: 'top',
                  formatter (p) {
                    return p.value > 0 ? p.value : ''
                  }
                }
              }
            },
            // data: [
            //   1036,
            //   3693,
            //   2962,
            //   3810,
            //   2519,
            //   1915,
            //   1748,
            //   4675,
            //   6209,
            //   4323,
            //   2865,
            //   4298
            // ]
            // data: this.strangerData
            data: peopleNums
          }
        ]
      })
    }
  }
}
</script>
<style>
.chart-container {
  position: relative;
}

.chart-button {
  position: absolute;
  top: 450px;
  /* 控制按钮距离顶部的距离 */
  /* right: 860px; */
  left: 300px;
  /* 控制按钮距离右侧的距离 */
  /* 这里可以定义其他样式属性，如颜色、大小等 */
}
</style>